<?php
namespace App\Controllers\Index;
use App\Models\Peizhi\Peizhi;
use App\Models\Goods\Goods;

/**
*	首页接口获取
*/
class Index extends \App\Libs\Base\BaseAction
{
    protected function beforeRun() {
        parent::beforeRun();
        //$this->needLogin = TRUE;
    }
	
	public function execute(){
		$city = $this->request->get('city');
		//查询peizhi表里所有首页的数据
		$oneTwoThere = Peizhi::findAll('type=1 or type = 2 or type = 3');
		foreach($oneTwoThere as $k=>$v){
			if( $v->type == 1 ){
				$peizhi['banner'] = json_decode($v->data);
			}
			if( $v->type == 2 ){
				$peizhi['active'] = json_decode($v->data);
			}
			if( $v->type == 3 ){
				$peizhi['icon'] = json_decode($v->data);
			}
		}
		//$peizhi['activity'] = Peizhi::findFirst('type=2')->data;
		//$peizhi['icon'] = Peizhi::findFirst('type=3');
		//爆款处理
	        $baokuan = Peizhi::findFirst('type=4');	
		//爆款推荐
		//echo "<pre>";print_r($baokuan);
		$baokuanArr = json_decode($baokuan->data);
		$midBaoKuan = [];
		if(!empty( $baokuanArr->$city )){
			
			foreach( $baokuanArr->$city as $k=>$v ){
		//	echo "<pre>";print_r($v);
				$where[] = 'id ='.$v->gid;
				$baokuanOther[$v->gid] = $v;
			}

		   $midBaoKuan = Goods::findAll(array(implode(' or ', $where),"order" => "weight"))->toArray();
      		  //更改价格；
      		  foreach($midBaoKuan as $key => $val){
        		    $priceArray['goods_price'] = Goods::formatPrice($val['goods_price']);
        		    $priceArray['sale_price'] = Goods::formatPrice($val['sale_price']);
          		  $midBaoKuan[$key] = array_merge($val,$priceArray);
        		}

		}
		$peizhi['baokuan'] =$midBaoKuan;
		foreach($peizhi['baokuan'] as $k=>$v){
			$peizhi['baokuan'][$k]['addr'] = $baokuanOther[$v['id']]->addr;
			$peizhi['baokuan'][$k]['img'] = $baokuanOther[$v['id']]->img;
			$peizhi['baokuan'][$k]['action'] = $baokuanOther[$v['id']]->action;
		}
		return \App\Libs\Views\Json::view(0, '成功', $peizhi);	
	}
}
